/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.system.AdminOperRelaDao;
import org.grayrabbit.cms.entity.Admin;
import org.grayrabbit.cms.entity.AdminOperRela;
import org.grayrabbit.cms.vo.system.AdminOperRelaBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>AdminOperRela</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:55:12 
 */
@Repository
public class AdminOperRelaDaoImpl extends GenericHibernateDao<AdminOperRela, String>
		implements AdminOperRelaDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From AdminOperRela as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";
	

	/**
	 * 根据bean组装查询条件
	 * @see AdminOperRelaDao#assembly(AdminOperRelaBean)
	 */
	public QueryCondition assembly(AdminOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see AdminOperRelaDao#assemblyCount(AdminOperRelaBean)
	 */
	public QueryCondition assemblyCount(AdminOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see AdminOperRelaDao#count(AdminOperRelaBean)
	 */
	public int count(AdminOperRelaBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see AdminOperRelaDao#assemblyNameQuery(AdminOperRelaBean)
	 */
	public QueryCondition assermblyNameQuery(AdminOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see AdminOperRelaDao#assemblyNameQueryCount(AdminOperRelaBean)
	 */
	public QueryCondition assemblyNameQueryCount(AdminOperRelaBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see AdminOperRelaDao#findByPage(Page, AdminOperRelaBean)
	 */
	public List<AdminOperRela> findByPage(Page<AdminOperRela> page,
			AdminOperRelaBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(AdminOperRelaBean bean,QueryCondition qc){
		if(bean!=null){
		}
	}

	public List<AdminOperRela> findAllAuthCodeByAdminId(String adminId) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(adminId)) 
			qc.add(" And obj.admin.adminId  = ?",adminId);
		return findByQc(qc);
	}

	public List<AdminOperRela> findObjectAuth(String targetId, String targetType){
		if(StringUtils.isBlank(targetId) || StringUtils.isBlank(targetType)) 
			   throw new IllegalArgumentException(" targetId or targetType   can't be  blank!");
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.operTargetType = ? ", targetType);
		qc.add(" And obj.operTargetId like ?", "%"+targetId+"%");
		qc.append(" And ( ");
		qc.add(" obj.operTargetId like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId2 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId3 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId4 like ?", "%"+targetId+"%");	
		qc.add("  or obj.operTargetId5 like ?", "%"+targetId+"%");	
		qc.append(" ) ");
		return findByQc(qc) ;
	}

	public List<AdminOperRela> findByTargetIdAndOperCodeId(String adminId,
			String operId, String operTargetId) {
		if(StringUtils.isBlank(operId)) 
			throw new IllegalArgumentException("operId or operTargetId   can't be  blank!");
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(adminId)){
			qc.add(" And obj.admin.adminId = ? ", adminId);
		}
			
		qc.add(" And obj.operCode.operId = ? ", operId);
		if(StringUtils.isNotBlank(operTargetId)){
			qc.append(" And ( ");
			qc.add(" obj.operTargetId like ?", "%"+operTargetId+"%");	
			qc.add("  or obj.operTargetId2 like ?", "%"+operTargetId+"%");	
			qc.add("  or obj.operTargetId3 like ?", "%"+operTargetId+"%");	
			qc.add("  or obj.operTargetId4 like ?", "%"+operTargetId+"%");	
			qc.add("  or obj.operTargetId5 like ?", "%"+operTargetId+"%");	
			qc.append(" ) ");
		}
		return findByQc(qc);
	}

	public List<Admin> findCategoryAuditorsAdmin(String codeId, String targetId) {
		List<AdminOperRela> list = findByTargetIdAndOperCodeId(null, codeId, targetId);
		List<Admin> adminList = new ArrayList<Admin>();
		for(AdminOperRela operRela:list){
			adminList.add(operRela.getAdmin());
		}
		return adminList;
	}
  

}